<template>
  <el-form :model="formData" label-width="120px" ref="form" v-loading="loading" class="pr-4" >
    <el-form-item label="套餐名称" prop="name" :rules="[{required: true,message: '昵称必须填写'}]">
      <el-input v-model="formData.name" name="name" clearable />
    </el-form-item>
    <el-form-item label="套餐简介" prop="mini_text" :rules="[{required: true,message: '套餐简介必须填写'}]">
      <el-input v-model="formData.mini_text" name="mini_text" clearable />
    </el-form-item>
    <el-form-item label="套餐价格" prop="price" :rules="[{required: true,message: '套餐价格必须填写'}]">
      <el-input v-model="formData.price" name="price" clearable />
    </el-form-item>
    <!-- <el-form-item label="外部套餐编码" prop="out_package_id" :rules="[{required: true,message: '外部套餐编码必须填写'}]">
      <el-input v-model="formData.out_package_id" name="out_package_id" clearable />
    </el-form-item> -->
    <el-form-item label="自定义标题" prop="my_title" >
      <el-input v-model="formData.my_title" name="my_title" clearable />
    </el-form-item>
    <el-form-item label="运营商" prop="operator_id" >
      <el-input v-model="formData.operator_id" name="operator_id" clearable />
    </el-form-item>
    <el-form-item label="自定义背景色" prop="color" :rules="[{required: true,message: '自定义背景色必须填写'}]">
      <!-- <el-input v-model="formData.color" name="color" clearable /> -->
      <el-color-picker v-model="formData.color" size="large"  />
    </el-form-item>
    <el-form-item label="编码" prop="up_product_code" :rules="[{required: true,message: '编码必须填写'}]">
      <el-input v-model="formData.up_product_code" name="up_product_code" clearable />
    </el-form-item>
    <el-form-item label="号库" prop="uporlocal" :rules="[{required: true,message: '号库必选'}]">
      <el-select v-model="formData.uporlocal" placeholder="请选择">
        <el-option
          v-for="item in uporlocalStatus"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="套餐头图" prop="head_img" :rules="[{required: true,message: '套餐头图必填'}]">
      <Upload v-model="formData.head_img" />
    </el-form-item>
    <el-form-item label="套餐详情" prop="details" :rules="[{required: true,message: '套餐详情必填'}]">
      <!-- <Uploads v-model="formData.details" /> -->
      <Editor v-model="formData.details" style="z-index: 1;"></Editor>
    </el-form-item>
    <el-form-item label="地址拦截" prop="details">
      <!-- <Uploads v-model="formData.details" /> -->
      <el-button type="primary" @click="setAddress">设置</el-button>
    </el-form-item>
    <div class="flex justify-end">
      <el-button type="primary" @click="submitForm(form)">{{ $t('system.confirm') }}</el-button>
    </div>
    <el-dialog  v-model="addressVisible" title="地址拦截" style="z-index: 9999;margin-top: 300px;">
      <el-form :model="addressData" label-width="120px" ref="forms" v-loading="loading" class="pr-4" style="padding-top: 30px;">
        <el-form-item label="省拦截" prop="province">
          <el-input v-model="addressData.province" name="province" clearable />
          <p style="font-size: 14px;line-height: 16px;margin-top: 5px;color: gray;">请填写省名称前两位，如：江苏省填写江苏；内蒙古自治区填写内蒙；如有多个省份中间请用&字符隔开，如：云南&西藏&新疆</p>
        </el-form-item>
        <el-form-item label="市拦截" prop="city">
          <el-input v-model="addressData.city" name="city" clearable />
          <p style="font-size: 14px;line-height: 16px;margin-top: 5px;color: gray;">请填写省名称前两位+地市名称前两位，如：江苏镇江、云南昆明</p>
        </el-form-item>
        <el-form-item label="区拦截" prop="area">
          <el-input v-model="addressData.area" name="area" clearable />
          <p style="font-size: 14px;line-height: 16px;margin-top: 5px;color: gray;">请填写省名称前两位+地市名称前两位+区县名称前两位</p>
        </el-form-item>
        <el-form-item label="年龄拦截" prop="age">
          <el-input v-model="addressData.age" name="age" clearable />
          <p style="font-size: 14px;line-height: 16px;margin-top: 5px;color: gray;">填写规则：18-59，即拦截小于18岁，大于59岁</p>
        </el-form-item>
        <el-form-item label="是否开启拦截" prop="status">
          <el-select v-model="addressData.status" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <div class="flex justify-end">
          <el-button type="primary" @click="submit(form)">设置</el-button>
        </div>
      </el-form>
    </el-dialog>
  </el-form>
</template>

<script lang="ts" setup>
import { useCreate } from '@/composables/curd/useCreateTwo'
import { useShow } from '@/composables/curd/useShow'
import { onMounted , ref } from 'vue'
import {  ElDialog, ElButton, ElPopover } from 'element-plus';
import Message from '@/support/message'
import http from '@/support/http'

const props = defineProps({
  primary: [String, Number],
  api: String,
})

// const addressData = ref<object>({province:'',city:'',area:'',status:2,age:''})
const { formData, form, loading, submitForm, close , addressData , isSet } = useCreate(props.api, props.primary)

const options =  ref([{ value: '1', label: '开启' },{value:'2',label:'不开启'}]);

const uporlocalStatus = ref([{ value: 1, label: '无' },{value:2,label:'本地'},{value:3,label:'线上'}])
if (props.primary) {
  useShow(props.api, props.primary, formData)
}else{
}

const addressVisible = ref(false)
const setAddress = () => {
  addressVisible.value = true
}
// if(id){
            //     http.put('/intercept/intercept/' + formData.value.addressId,unref(formData)).then(res => {  

            //     })
            // }else{
            //     http.post('/intercept/intercept',unref(formData)).then(res => {

            //     })
            // }
const submit = (val) => {
  if (props.primary) {
    console.log(val,formData.value.id)
    addressData.value.package_id = formData.value.id
    if(isSet.value){
      http.put('/intercept/intercept/' + addressData.value.id,addressData.value).then(res => {  
        if(res.data.code == 10000){
          Message.success(res.data.message)
          addressVisible.value = false
        }
      })
      
    }else{
      http.post('/intercept/intercept',addressData.value).then(res => {
        if(res.data.code == 10000){
          Message.success(res.data.message)
          addressVisible.value = false
        }
      })
    }
  }else{
    addressVisible.value = false
  }
  
}

const emit = defineEmits(['close'])
onMounted(() => {
  close(() => emit('close'))
})
</script>